package com.yuanfeng.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.business.entity.ShopAvgEvaluationEntity;
import com.yuanfeng.business.entity.ShopEvaluationEntity;
import com.yuanfeng.business.mapper.ShopAvgEvaluationMapper;
import com.yuanfeng.business.mapper.ShopEvaluationMapper;
import com.yuanfeng.business.service.ShopEvaluationService;
import com.yuanfeng.commoms.constant.CommonStatusEnum;
import com.yuanfeng.commoms.dto.shop.ShopPercentDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseMapUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.shop.ShopEvaluationVO;
import com.yuanfeng.commoms.vo.shop.WebShopListQueryResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class ShopEvaluationServiceImpl extends ServiceImpl<ShopEvaluationMapper, ShopEvaluationEntity>
    implements ShopEvaluationService{

    @Autowired
    private ShopAvgEvaluationMapper shopAvgEvaluationMapper;
    @Override
    public ResponseResult saveShopEvaluationInfo(Map<String, Object> param) {
        ShopEvaluationEntity evaluationEntity = new ShopEvaluationEntity();

        evaluationEntity = ParseMapUtils.parseMap2Object(param, ShopEvaluationEntity.class);
        Boolean flag = this.baseMapper.insert(evaluationEntity) == CommonStatusEnum.SUCCESS.getCode();
        if (flag) {
            return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
        } else {
            return ResponseResult.fail(BizCodeEnume.OPERATION_FAILED);
        }
    }

    @Override
    public List<WebShopListQueryResultVO> queryShopScore(List<WebShopListQueryResultVO> param) {
        for(int i = 0;i<param.size();i++){
            Integer shopId = param.get(i).getShopId();
            ShopAvgEvaluationEntity see = shopAvgEvaluationMapper.selectOne(new LambdaQueryWrapper<ShopAvgEvaluationEntity>()
                                      .eq(ShopAvgEvaluationEntity::getShopId,shopId));
            if(see != null){
                double servicecreditScore = see.getAvgServicecredit();
                double desccreditScore = see.getAvgDesccredit();
                double deliverycreditScore = see.getAvgDeliverycredit();
                if(String.valueOf(servicecreditScore) == null || servicecreditScore == 0){
                    see.setEvaluationServicecredit(5);
                }else{
                    see.setEvaluationServicecredit(see.getAvgServicecredit());
                }
                if(String.valueOf(desccreditScore) == null || desccreditScore == 0){
                    see.setEvaluationDesccredit(5);
                }else{
                    see.setEvaluationDesccredit(see.getAvgDesccredit());
                }
                if(String.valueOf(deliverycreditScore) == null || deliverycreditScore == 0){
                    see.setEvaluationDeliverycredit(5);
                }else{
                    see.setEvaluationDeliverycredit(see.getAvgDeliverycredit());
                }
                ShopPercentDTO sp = shopAvgEvaluationMapper.queryShopPercent(servicecreditScore,desccreditScore,deliverycreditScore);
                String servicecreditNum = String.format("%.2f",(Double.parseDouble(sp.getServicecreditPercent()) * 100)) + "%";
                String desccreditNum = String.format("%.2f",(Double.parseDouble(sp.getDesccreditPercent()) * 100)) + "%";
                String deliverycreditNum = String.format("%.2f",(Double.parseDouble(sp.getDeliverycreditPercent()) * 100)) + "%";
                sp.setDeliverycreditPercent(deliverycreditNum);
                sp.setDesccreditPercent(desccreditNum);
                sp.setServicecreditPercent(servicecreditNum);
                see.setShopPercent(sp);
                ShopEvaluationVO vo=new ShopEvaluationVO();
                BeanUtils.copyProperties(vo,see);
                param.get(i).setShopEvaluationEntity(vo);
            }else{
                ShopEvaluationEntity sees = new ShopEvaluationEntity();
                ShopPercentDTO sp = new ShopPercentDTO();
                sees.setEvaluationDeliverycredit(5);
                sees.setEvaluationDesccredit(5);
                sees.setEvaluationServicecredit(5);
                sp.setServicecreditPercent("100%");
                sp.setDesccreditPercent("100%");
                sp.setDeliverycreditPercent("100%");
                sees.setShopPercent(sp);
                ShopEvaluationVO vo=new ShopEvaluationVO();
                BeanUtils.copyProperties(vo,see);
                param.get(i).setShopEvaluationEntity(vo);
            }
        }
        return param;
    }
}




